package frobagroup.services.backstage.impl;

import frobagroup.entities.backstage.CarouselFigure;
import frobagroup.repositories.backstage.CarouselFigureRepository;
import frobagroup.services.backstage.StaffCarouselFigureService;
import frobagroup.utils.fileUtils.FileUploadServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Tinsley on 2017/6/19.
 */
@Service
public class StaffCarouselFigureServiceImpl implements StaffCarouselFigureService {

    @Autowired
    private CarouselFigureRepository carouselFigureRepository;
    @Autowired
    private FileUploadServer fileUploadServer;

    @Override
    public CarouselFigure addOrEditCarousel(MultipartFile pic, String sId, String pId, String cId,
                                            String describe,String whichType,String type) throws Exception{
        String picUrl = "";
        boolean wt = Boolean.parseBoolean(whichType);
        if (!StringUtils.isEmpty(pic))
            picUrl = fileUploadServer.uploadFileToCloud(pic);
        CarouselFigure carouselFigure;
        if (StringUtils.isEmpty(cId))
            carouselFigure = new CarouselFigure(picUrl, sId, describe,wt);
        else{
            carouselFigure = carouselFigureRepository.findOne(cId);
            if (!StringUtils.isEmpty(picUrl)) {
                if (!StringUtils.isEmpty(carouselFigure.getPic())){
                    fileUploadServer.deletePublicFile(carouselFigure.getPic());
                }
                carouselFigure.setPic(picUrl);
            }
            carouselFigure.setUpdateUid(sId);
            carouselFigure.setUpdateTime(new Date());
            carouselFigure.setProductId(pId);
            carouselFigure.setDescribe(describe);
        }

        if (!StringUtils.isEmpty(pId))
            carouselFigure.setProductId(pId);
        else
            carouselFigure.setProductId(null);

        if (!StringUtils.isEmpty(type))
            carouselFigure.setType(type);
        return carouselFigureRepository.save(carouselFigure);
    }

    @Override
    public List<CarouselFigure> findCarousels(Map<String, String> params) {
        boolean whichType = Boolean.parseBoolean(params.get("whichType"));
        List<CarouselFigure> cfs;
        if (StringUtils.isEmpty(params.get("status")))
                cfs = carouselFigureRepository.findByWhichType(whichType);
        else
            cfs = carouselFigureRepository.findByStatusAndWhichType(params.get("status"),whichType);
        return cfs.parallelStream().sorted(Comparator.comparing(CarouselFigure::getCreateTime).reversed()).collect(Collectors.toList());
    }

    @Override
    public boolean changeStatus(String cId, String status, String sId) {
       CarouselFigure carouselFigure =  carouselFigureRepository.findOne(cId);
        if (StringUtils.isEmpty(carouselFigure))
            return Boolean.FALSE;
        carouselFigure.setStatus(status);
        carouselFigure.setUpdateTime(new Date());
        carouselFigure.setUpdateUid(sId);
        carouselFigureRepository.save(carouselFigure);
        return Boolean.TRUE;
    }

    @Override
    public void remove(String id,String picUrl) throws Exception{
        if (!StringUtils.isEmpty(picUrl))
            fileUploadServer.deletePublicFile(picUrl);
        carouselFigureRepository.delete(id);
    }
}
